package com.spring.appservice.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.common.collect.Maps;
import com.spring.appservice.web.requestModel.BaseFile;
import com.spring.appservice.web.requestModel.Journal;
import com.spring.appservice.web.requestModel.JournalUser;
import com.spring.appservice.web.service.MainShowService;
import com.spring.myspace.web.service.MySpaceService;
import com.spring.security.core.annotation.Log;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.controller.BaseController;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value = "/zone")
//@ApiIgnore
public class ZoneController extends BaseController{
	@Resource 
	private MainShowService mainShowService;
	@Resource MySpaceService mySpaceService;
	
	

		//获取时间轴资源 接口作废
    //@RequestMapping(value = "/zoneInfo")
    @ResponseBody
    public Object zoneInfo(String token,String zoneType,String callback) throws Exception{
    	BaseResult ret = new BaseResult();
    	try{
    		ret = mainShowService.zoneInfo(token, zoneType);
    	}catch(Exception e){
    		ret.setStatus("0");
    		ret.setMsg("系统异常");
    		e.printStackTrace();
    	}
    	
    	return buildResultInfo(ret);
    }
	
	
	//获取时间轴资源
    @RequestMapping(value = "/updateZoneInfo")
    @ResponseBody
    public Object updateZoneInfo(String babyID,String zoneType, String title, MultipartFile avatar, MultipartFile cover,String token,String callback) throws Exception{
    	Map<String,Object> rt = Maps.newHashMap();
    	try{
    		rt = mainShowService.updateZoneInfo(babyID,zoneType, title, avatar, cover, token);
    	}catch(Exception e){
    		rt.put(ApplicationUtils.STATUS, "0");
    		rt.put(ApplicationUtils.MSG, "系统异常");
    		e.printStackTrace();
    	}
        if(ApplicationUtils.isNotNull(callback)){
        	return new JSONPObject(callback, rt);
        }else{
        	return rt;
        }
    }
	
	
	//获取时间轴资源
    @ApiOperation(value = "获取时间轴接口", httpMethod = "GET", response = Object.class, 
    		notes = "获取时间轴接口",produces = "application/json; charset=utf-8")       
    @RequestMapping(value = "/timeline", method = RequestMethod.GET)
    @ResponseBody
    public Object timeline(String babyID,String pageSize,String pageIndex,String skip,String rows,String token,String callback) throws Exception{
    	Map<String,Object> rt = Maps.newHashMap();
    	try{
    		rt = mainShowService.timeline(babyID,pageSize,pageIndex,skip, rows, token,"0");
    	}catch(Exception e){
    		rt.put(ApplicationUtils.STATUS, "0");
    		rt.put(ApplicationUtils.MSG, "系统异常");
    		e.printStackTrace();
    	}
        if(ApplicationUtils.isNotNull(callback)){
        	return new JSONPObject(callback, rt);
        }else{
        	return rt;
        }
    }
    
    
	@RequestMapping(value = "/getBabyListOrderByName", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取宝宝列表接口", httpMethod = "GET", response = Object.class, notes = "获取宝宝列表接口按拼音首字母排序",produces = "application/json; charset=utf-8")    
    @Log(value="获取宝宝列表接口")
    public Object getBabyListOrderByName(@ApiParam(required = false, name = "kidsParkID", value = "乐园id")@RequestParam String kidsParkID) throws Exception{
		BaseResult rt = null;
    	try{
    		rt = mainShowService.getBabyListOrderByName(kidsParkID);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }

	
/*	@RequestMapping(value = "/teacherCreateJournal", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "教师发日志", httpMethod = "POST", response = Object.class, notes = "教师发日志 mapAppType:gaode baidu ,osType 1 安卓 2 ios ",produces = "application/json; charset=utf-8")    
    @Log(value="教师发日志")
    public Object teacherCreateJournal(@RequestBody Journal jouranl) throws Exception{
		BaseResult rt = null;
    	try{
    		rt = mainShowService.teacherCreateJournal(jouranl);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }*/
	
	
	@RequestMapping(value = "/teacherCreateJournal", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "教师发日志", httpMethod = "POST", response = Object.class, notes = "教师发日志 mapAppType:gaode baidu ,osType 1 安卓 2 ios user=[{cFamilyID:xxx,cUserID:xxxx}] 图片视频另起接口上传",produces = "application/json; charset=utf-8")    
    @Log(value="教师发日志")
    public Object teacherCreateJournal(Journal jouranl,
    		@ApiParam(required = false, name = "photo", value = "图片")@RequestParam(value="photo",required = false)String photo,
    		@ApiParam(required = false, name = "user", value = "用户")@RequestParam(value="user",required = false)String user,
    		@ApiParam(required = false, name = "video", value = "视频")@RequestParam(value="video",required = false)String video    		
    		) throws Exception{
		//注入用户信息
		if(user!=null){
			List<JournalUser> userList=new ArrayList<JournalUser>();
			JSONArray json = JSONArray.fromObject(user);
			for (int i = 0; i < json.size(); i++) {
				JSONObject jsonObject = json.getJSONObject(i);
				String cFamilyID = MyTools.ObjectToString(jsonObject.get("cFamilyID")) ;
				String cUserID = MyTools.ObjectToString(jsonObject.get("cUserID"));
				JournalUser ju=new JournalUser();
				ju.setcFamilyID(cFamilyID);
				ju.setcUserID(cUserID);
				userList.add(ju);
			}
			jouranl.setUserList(userList);
		}

		BaseResult rt = null;
    	try{
    		rt = mainShowService.teacherCreateJournal(jouranl);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }	
	
	
	
	
	@RequestMapping(value = "/teacherAddPhoto", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "教师发日志", httpMethod = "POST", response = Object.class, notes = "教师发日志加图片file=[{\"file_id\":\"xxx\"}] ",produces = "application/json; charset=utf-8")    
    @Log(value="教师发日志")
    public Object teacherAddPhoto(
    		@ApiParam(required = false, name = "my_space_journal_id", value = "日志id")@RequestParam(value="my_space_journal_id",required = false) String my_space_journal_id,
    		@ApiParam(required = false, name = "file", value = "file")@RequestParam(value="file",required = false) String file,
    		@ApiParam(required = false, name = "token", value = "token")@RequestParam(value="token",required = false) String token) throws Exception{
		BaseResult rt = null;
    	try{
    		rt = mainShowService.teacherAddPhoto(my_space_journal_id, file, token);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }
	
	@RequestMapping(value = "/teacherAddVideo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "教师发日志", httpMethod = "POST", response = Object.class, notes = "教师发日志加视频",produces = "application/json; charset=utf-8")    
    @Log(value="教师发日志")
    public Object teacherAddVideo(
    		@ApiParam(required = false, name = "my_space_journal_id", value = "日志id")@RequestParam(value="my_space_journal_id",required = false) String my_space_journal_id,
    		@ApiParam(required = false, name = "fileID", value = "文件id")@RequestParam(value="fileID",required = false) String fileID,
    		@ApiParam(required = false, name = "token", value = "token")@RequestParam(value="token",required = false) String token) throws Exception{
		BaseResult rt = null;
    	try{
    		rt = mainShowService.teacherAddVideo(my_space_journal_id, fileID, token);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }

	@RequestMapping(value = "/deleteJournal")
	@ResponseBody
	@ApiOperation(value = "家长删日志", httpMethod = "POST", response = Object.class, notes = "家长删日志",produces = "application/json; charset=utf-8")    	   
	public Object  deleteJournal(
			@ApiParam(required = false, name = "token", value = "token")@RequestParam(value="token",required = true)String token,
			@ApiParam(required = false, name = "my_space_journal_id", value = "日志id")@RequestParam(value="my_space_journal_id",required = true)String my_space_journal_id) {
		Map<String,Object> rt = Maps.newHashMap();
		try {
			rt = mySpaceService.deleteJournal(token, my_space_journal_id);
		} catch (Exception e) {
			rt.put(ApplicationUtils.STATUS, "0");
			rt.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();
		}
		return rt;		
	}

	
	@RequestMapping(value = "/deleteTeacherJournal", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "教师删日志", httpMethod = "POST", response = Object.class, notes = "教师删日志 ",produces = "application/json; charset=utf-8")    
    @Log(value="教师删日志")
    public Object deleteTeacherJournal(
    		@ApiParam(required = false, name = "token", value = "token")@RequestParam(value="token",required = false)String token,
			@ApiParam(required = false, name = "my_space_journal_id", value = "日志id")@RequestParam(value="my_space_journal_id",required = false)String my_space_journal_id
    		) throws Exception{
		BaseResult rt = null;
    	try{
    		rt = mySpaceService.deleteTeacherJournal(token, my_space_journal_id);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }
	

	@RequestMapping(value = "/getTeacherJournal", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "教师查日志", httpMethod = "GET", response = Object.class, notes = "教师查日志",produces = "application/json; charset=utf-8")    	   
	 public Object getTeacherJournal(
			 @ApiParam(required = false, name = "pageSize", value = "分页参数")@RequestParam(value="pageSize",required = false)String pageSize,
			 @ApiParam(required = false, name = "pageIndex", value = "分页参数 ")@RequestParam(value="pageIndex",required = false)String pageIndex,
			 @ApiParam(required = false, name = "skip", value = "分页参数 ")@RequestParam(value="skip",required = false)String skip,
			 @ApiParam(required = false, name = "rows", value = "分页参数 ")@RequestParam(value="rows",required = false) String rows,
			 @ApiParam(required = false, name = "token", value = "token ")@RequestParam(value="token",required = false) String token) throws Exception{
    	Map<String,Object> rt = Maps.newHashMap();
    	try{
    		rt = mainShowService.timeline(null,pageSize,pageIndex,skip, rows, token,"1");
		} catch (Exception e) {
			rt.put(ApplicationUtils.STATUS, "0");
			rt.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();
		}

    	return rt;
	}
    
}
